package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.entity.Local;
import com.xca.znb.model.impl.LocalManagerImpl;
import com.xca.znb.presenter.LocalManagerPresenter;

import java.util.ArrayList;
import java.util.List;

public class LocalCartActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView Back ;
    private ListView listLocal ;
    private Button AddLocal ;
    private List<Local> localList ;
    private ArrayAdapter<Local> adapter ;
    private LocalManagerPresenter presenter ;
    private boolean isDataUpdated = false;

    private ArrayList<Integer> idList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_local_manager);
        Back = findViewById(R.id.my_local_manager_back) ;
        listLocal = findViewById(R.id.my_local_list) ;
        AddLocal = findViewById(R.id.add_local_btn) ;
        Back.setOnClickListener(this);
        AddLocal.setOnClickListener(this);

        LocalManagerImpl localManager = new LocalManagerImpl(this);
        presenter = new LocalManagerPresenter(localManager);


        if (getIntent().getExtras() != null) {
            // 使用Serializable方式获取ArrayList
            idList = (ArrayList<Integer>) getIntent().getSerializableExtra("cartLocal_idList");
        }

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.my_local_manager_back){
            Intent intent = getIntent();
            int localId = intent.getIntExtra("cartLocal_localId" ,0);
            Intent intent1 = new Intent(LocalCartActivity.this, CartClearing.class);
            intent1.putExtra("cart_list_id" , idList);
            intent1.putExtra("cart_local_id",localId);
            startActivity(intent1);
            finish();
        } else if (v.getId() == R.id.add_local_btn) {
            Intent intent = new Intent(this , AddPayLocalActivity.class) ;
            startActivity(intent);
        }
    }

    private void refreshData(){
        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");


        localList = presenter.getLocalByPhone(phone);
        adapter = new ArrayAdapter<Local>(this , R.layout.local_list_item , R.id.localList_show_local , localList){
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position , converView , parent);
                LinearLayout show = view.findViewById(R.id.localList_show);
                TextView showLocal = view.findViewById(R.id.localList_show_local);
                TextView showId = view.findViewById(R.id.localList_show_id) ;
                TextView showName = view.findViewById(R.id.localList_show_name);
                TextView showPhone = view.findViewById(R.id.localList_show_phone) ;
                TextView showDefault = view.findViewById(R.id.localList_show_default) ;
                ImageButton goUpdate = view.findViewById(R.id.localList_update_btn);
                Local local = getItem(position);
                if (local != null) {
                    showLocal.setText(local.getAddress());
                    showId.setText(String.valueOf(local.getId()));
                    showName.setText(local.getName());
                    showPhone.setText(local.getPhone());
                    if (local.getType().equals("是")){
                        showDefault.setText("默认");
                    }
                    goUpdate.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(LocalCartActivity.this , UpdatePayLocalActivity.class);
                            intent.putExtra("myLocalId" , local.getId());
                            startActivity(intent);
                        }
                    });
                    show.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(LocalCartActivity.this , CartClearing.class);
                            intent.putExtra("cart_list_id" , idList);
                            intent.putExtra("cart_local_id" , local.getId());
                            startActivity(intent);
                            finish();
                        }
                    });
                }


                return view ;
            }
            public void add(Local local){
                localList.add(local);
                notifyDataSetChanged();
            }
            public void remove(Local local){
                localList.remove(local);
                notifyDataSetChanged();
            }
            public void update(int position , Local local){
                localList.set(position,local);
                notifyDataSetChanged();
            }
        };

        listLocal.setAdapter(adapter);
    }
    @Override
    protected void onResume() {
        super.onResume();
        refreshData();
    }
}
